require.config({
                baseUrl:"../",
               paths:{
               bsl:'bsl',
               
               }
               });
require(['bsl'],function(bsl){
        document.addEventListener('deviceready', onDeviceready, false);
        function onDeviceready(){
        alert("star");
        function createC(){
        var name=document.getElementById("name").value;
        var phoneNumbers=document.getElementById("number").value;
        var myContact = bsl.contacts.create();
        myContact.displayName=name;
        myContact.phoneNumbers = [{type:"phone",value:phoneNumbers,preferred:true}];
        function onSaveSuccess(){
        alert("The contact name:" + myContact.displayName+",number:"+myContact.phoneNumbers.value);
        }
        function onSaveError(){
        alert("The error");
        }
//         myContact.save(onSaveSuccess,onSaveError);
        }
        
        function findC(){
        var name=document.getElementById("name").value;
        var phoneNumbers=document.getElementById("number").value;
        var options = new ContactFindOptions();
        options.filter=name;
        options.multiple=true;// show all
        var fields = ["displayName", "name"];
        function success(contactsArray) {
        alert("length:"+contactsArray.length);
        for (var i=0; i<contactsArray.length; i++) {
        alert("Display Name = " + contactsArray[i].displayName);
        }
        }
        function error(msg) {
        alert('onError!'+msg);
        }
        bsl.contacts.find (fields, success, error, options);
        }
        function findAllC(){
        //        var name=document.getElementById("name").value;
        //var phoneNumbers=document.getElementById("number").value;
        var options = new ContactFindOptions();
        options.filter="";
        options.multiple=true;
        var fields = ["phoneNumbers", "displayName"];
        //        function success(contactsArray) {
        //        alert("length:"+contactsArray.length);
        //        for (var i=0; i<contactsArray.length; i++) {
        //        alert("Display Name = " + contactsArray[i].name);
        //        }
        //        }
        //获取字符串字节数
        String.prototype.getBytesLength = function() {
        return this.replace(/[^\x00-\xff]/gi, "--").length;
        };
        
        
        function contacts_success(contacts) {
        
        
        
        var subStrByCnLen = function(str, len) {
        
        var cnlen = len * 2;
        
        var index = 0;
        var tempStr = "";
        
        for (i = 0; i < str.length; i++) {
        var s = str.charAt(i);
        if (index >= cnlen) {
        // return tempStr;
        } else {
        if (s.getBytesLength() > 1) {
        index += 2;
        } else {
        index += 1;
        }
        tempStr = tempStr + s;
        }
        }
        if (str.getBytesLength() > cnlen) {
        tempStr = tempStr + "..";
        }
        return tempStr;
        };
        //alert("13");
        var contactsStr = "";
        var phonesstr = "";
        var showStr="";
        console.log("----------"+contacts.length);
        for (var ii = 0; ii < contacts.length; ii++) {
        //alert(i);
        if (contacts[ii].phoneNumbers) {
        //alert(contacts[i].phoneNumbers);
        //alert(i+"存在");
        if (contacts[ii].phoneNumbers[0]) {
        //alert(i+"存在"+"000");
        } else {
        //alert(i+"不存在"+"000");
        contacts[ii].phoneNumbers[0] = {
        value: ""
        };
        }
        } else {
        //alert(i+"不存在");
        contacts[ii].phoneNumbers = new Array();
        contacts[ii].phoneNumbers[0] = {
        value: ""
        };
        }
        
        var phoneNum = contacts[ii].phoneNumbers[0].value;
        //alert(phoneNum+" "+ii);
        var shortname = subStrByCnLen(contacts[ii].displayName, 6);
        //alert(shortname)
        contactsStr = contactsStr + shortname + "<br>";
        //alert("2");
        showStr=showStr+shortname+":";
        phonesstr = phonesstr + phoneNum + "<br>";
        showStr=showStr+phoneNum+ ";";
        }
        alert(showStr);

        }
        
        
        function error(msg) {
        alert('onError!'+msg);
        }
        bsl.contacts.find (fields, contacts_success, error, options);
        }
        
        
        document.getElementById("createContacts").addEventListener("click",createC,false);
        document.getElementById("findContacts").addEventListener("click",findC,false);
        document.getElementById("showAllContacts").addEventListener("click",findAllC,false);
        }
        
        });